
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class EquipAdd extends JFrame{
    public EquipAdd(String userid){

        SQLOperation sql=new SQLOperation();

        setTitle("Equip Add");
        Container container=getContentPane();
        setLayout(new BorderLayout());

        JPanel titlePanel=new JPanel();

        JLabel title=new JLabel("Please complete the following information.");
        title.setFont(new Font("",Font.BOLD,20));

        titlePanel.add(title);

        add(titlePanel,BorderLayout.NORTH);

        JPanel info=new JPanel(new GridLayout(3,1,5,5));




        JPanel typePanel=new JPanel();

        typePanel.add(new JLabel("     TypeName:"));
        JComboBox type=new JComboBox();
        type.addItem("----------------------");
        String[] typeAttribute={"typeID","tName"};
        Object[][] typeList=sql.search("DeviceType",typeAttribute,"shanchu",1);
        for(int i=1;i<=typeList.length;i++){
            type.addItem(typeList[i-1][1]);
        }
        typePanel.add(type);
        info.add(typePanel);



        JPanel factoryPanel=new JPanel();

        factoryPanel.add(new JLabel("FactoryName:"));
        JComboBox factory=new JComboBox();
        factory.addItem("----------------------");
        String[] factoryAttribute={"mUID"};
        Object[][] factoryList=sql.search("Manufacture",factoryAttribute,"shanchu",1);
        for(int i=1;i<=factoryList.length;i++){
            factory.addItem(factoryList[i-1][0]);
        }
        factoryPanel.add(factory);
        info.add(factoryPanel);



        JPanel familyPanel=new JPanel();

        familyPanel.add(new JLabel("        FamilyID:"));
        JComboBox family=new JComboBox();
        family.addItem("----------------------");
        String[] familyAttribute={"familyID"};
        Object[][] familyList=sql.search("Family",familyAttribute,"shanchu",1);
        for(int i=1;i<=familyList.length;i++){
            family.addItem(familyList[i-1][0]);
        }
        familyPanel.add(family);
        info.add(familyPanel);

        add(info,BorderLayout.CENTER);


        JButton back=new JButton("Back");
        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new EquipList(userid);
            }
        });

        JButton add=new JButton("Add");
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(type.getSelectedIndex()==0||factory.getSelectedIndex()==0){
                    JOptionPane.showMessageDialog
                            (null, "Please Fill in all information!","Missing Input",
                                    JOptionPane.ERROR_MESSAGE);

                }
                else {

                    String newPK = "";
                    boolean isSame = true;
                    while (isSame) {

                        isSame = false;
                        newPK = "id";


                        for (int i = 1; i <= 5; i++) {
                            newPK = newPK + ""+(char) ((int)(Math.random() * 10) + 48);
                        }
                        String[] pkID = {"idDevice"};
                        Object[][] pk = sql.search("Device", pkID, "shanchu", 1);
                        for (int i = 0; i < pk.length; i++) {
                            if (pk[i][0].equals(newPK)) {
                                isSame = true;
                                break;
                            }
                        }
                    }


                    Object[] newEquipInfo =
                            {newPK, 1, family.getSelectedItem(), typeList[type.getSelectedIndex()-1][0],
                                    factory.getSelectedItem()};

                    sql.add(newEquipInfo, "Device");


                    JOptionPane.showMessageDialog
                            (null, "Add Complete!Random idDevice:" + newPK,
                                    "Success", JOptionPane.INFORMATION_MESSAGE);

                    setVisible(false);

                    new EquipList(userid);
                }





            }
        });




        JPanel buttons=new JPanel();
        buttons.add(back);
        buttons.add(add);

        add(buttons,BorderLayout.SOUTH);






        setSize(500,240);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        setVisible(true);




    }
}
